package jdbc;

import jdbc.common.InputUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/*
    需求:
     1:程序启动后陆续要求用户输入:用户名，密码，昵称，年龄四个信息
     Scanner
     2:链接数据库
     3:将用户输入的信息包含在一条INSERT语句中
     4:执行该SQL语句将用户信息保存到userinfo表中
     5:提示用户注册成功
     6:关闭数据库链接
 */
public class RegUserDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        UserInfo userinfo = InputUtil.getInputObject(new UserInfo(),"欢迎注册");
        System.out.println(userinfo);
        //1:加载驱动
        // 加载MysqL驱动

        //2:通过DriverManager与数据库建立链接
        Connection connection = DBUtil.getConnection();
        System.out.println("与数据库连接成功！");
        Statement statement = connection.createStatement();
       String sql = "INSERT INTO userinfo(username,password,nickname,age) " +
               "VALUES('" + userinfo.getUsername() + "','" + userinfo.getPassword() + "','" + userinfo.getNickname() + "'," + userinfo.getAge() + ")";
        int num = statement.executeUpdate(sql);
        System.out.println(num > 0 ? "注册成功！" : "注册失败！");
        connection.close();


    }
}
